package com.example.demo.demos.web.controller;

import com.example.demo.demos.web.bean.utils.CacheUtil;
import com.example.demo.demos.web.comment.response.JsonResult;
import com.example.demo.demos.web.comment.response.StatusCode;
import com.example.demo.demos.web.mapper.WeiboMapper;
import com.example.demo.demos.web.pojo.dto.WeiboDTO;
import com.example.demo.demos.web.pojo.entity.Weibo;
import com.example.demo.demos.web.pojo.vo.UserVO;
import com.example.demo.demos.web.pojo.vo.WeiboDetailVO;
import com.example.demo.demos.web.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javassist.bytecode.stackmap.BasicBlock;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.ServiceConfigurationError;
import java.util.concurrent.TimeUnit;

@Validated
@Api(tags = "02.微博模块")
@Slf4j
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    @Autowired
    private CacheUtil cacheUtil;
    @ApiOperation(value = "发布微博功能")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO,@ApiIgnore HttpSession session){
        log.debug("weiboDTO = " + weiboDTO);
        UserVO userVO = (UserVO) session.getAttribute("user");
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insertWeibo(weibo);
        return JsonResult.ok();
    }
    @ApiOperation(value = "查看微博首页功能")
    @GetMapping("selectIndex")
    public JsonResult selectIndex(){
//        ValueOperations<String,Serializable> ops = redisTemplate.opsForValue();
//        Serializable weiboIndex = ops.get("weibo_index");
        Serializable weiboIndex = cacheUtil.get("weibo_index");
        if (weiboIndex == null){
            List<WeiboIndexVO> weiboIndexVOS = weiboMapper.selectWeiboIndex();
            System.out.println("首页数据来自MySQL数据库");
            cacheUtil.setExpire("weibo_index",(Serializable) weiboIndexVOS,30);
            return JsonResult.ok(weiboIndexVOS);
        }
        System.out.println("首页数据来自Redis数据库");
        return JsonResult.ok(weiboIndex);
//        List<WeiboIndexVO> weiboIndexVOS = weiboMapper.selectWeiboIndex();
//        return new JsonResult(2000,"操作成功",weiboIndexVOS);

    }
    @ApiOperation(value = "查看微博详情功能")
    @GetMapping("selectById")
    @ApiImplicitParam(name = "id",value = "微博编号",required = true,dataType = "long")
    public JsonResult selectById(@Range (min = 1,max = 500,message = "微博ID必须在1-500之间") @RequestParam Long id){
        if(id < 0){
            throw new IllegalArgumentException("微博编号不能小于零");
        }
        WeiboDetailVO weiboDetailVO = weiboMapper.selectWeiboById(id);
        return JsonResult.ok(weiboDetailVO);
    }
}
